#include <cserver/restapi.h>
#include <boost/log/trivial.hpp>
#include <boost/lexical_cast.hpp>
#include <pqxx/transaction.hxx>
#include <boost/format.hpp>

#include <sstream>

namespace csrv{ namespace webcmd{
	static const char* K_this_command= "api/v1/light/list_section";


	void light_list_energy(Request& req, pqxx::connection& dbconn, SessionContext& , const std::string& /*cmd*/ ){
		auto query = parseQuery(req.getParam("QUERY_STRING"));
		auto& callback = query["callback"];
		auto& euid = query["euid"];
		auto& from = query["fdate"];
		auto& to = query["tdate"];

		StreamGroup sg(req);
		auto& fout = sg.fout;
		response_header(sg.fout, 200);
		if (!callback.empty()) sg.fout << callback << "(";

		pqxx::work w(dbconn);
#if 1
		fout << boost::format(R"({"func":"light/list_energy",
"args":{},
"ret":{"success":true, "message":"",
"total":%1%,
"value":)") % TotalCount(w, "get_light_energy_total")(euid)(from)(to).result();
#endif
		auto result = w.prepared("get_light_energy")(euid)(from)(to).exec();
		fout << result2json(result, 3);
		fout << "}}";
		
		if (!callback.empty()) sg.fout << ")";
	}
}

static CommandAddHelper k_registerHandler("GET", webcmd::K_this_command, &webcmd::light_list_energy);
}

